package controllers

import (
	"baokaobang/datasourse"
	"baokaobang/models"
	"baokaobang/utils"
	"fmt"

	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/sessions"
	rc "github.com/rongcloud/server-sdk-go/v3/sdk"
)

type GroupMembers struct {
	Ctx iris.Context
}

//获取成员列表
func (c *GroupMembers) GetMembers() interface{} {
	channel := c.Ctx.Params().Get("channel")
	session := sessions.Get(c.Ctx)

	userId, _ := session.GetInt64(fmt.Sprintf("%s userId", channel))

	rongCloud := rc.NewRongCloud("x18ywvqfx4c1c", "Y3Vo93Qk1cC")
	if rongCloud == nil {
		return utils.CommonRes(1000, fmt.Sprintf("创建融云对象失败"), "")
	}

	//连接数据库
	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库：%s", err.Error()), "")
	}

	userInfo := new(models.User)

	//通过userId获取collegeCode
	ok, err := db.Table("user").ID(userId).Get(userInfo)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("获取用户信息失败: %s", err.Error()), "")
	}

	var rc rc.Group

	if ok {
		rc, err = rongCloud.GroupGet(userInfo.CollegeCode)
		if err != nil {
			return utils.CommonRes(1000, fmt.Sprintf("获取群成员信息失败: %s", err.Error()), "")
		}
	}

	return utils.CommonRes(20000, fmt.Sprintf("获取群成员信息成功"), rc)
}
